package com.chuanyang.driverh.mainmy;


import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.chuanyang.driverh.BaseApplication;
import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.base.OnCustomListener;
import com.chuanyang.driverh.mainmy.adapter.AcceptMemberAdapter;
import com.chuanyang.driverh.responseresult.CarTeamMenmberData;
import com.chuanyang.driverh.responseresult.CarTeamMenmberResult;
import com.chuanyang.driverh.responseresult.DriverInfoData;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;


public class TeamMenmberApplicationActivity extends BaseTitleActivity {
    private AcceptMemberAdapter adapter;
    private ArrayList<CarTeamMenmberData> menmberlist;
    private HashMap params;
    private DriverInfoData driverinfo;
    private ListView lv;

    @Override
    protected int getContentResId() {
        return R.layout.my_act_settingember;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        lv = (ListView) findViewById(R.id.ll_act_friend);
    }

    @Override
    protected void initData() {
        setBackImg();
        getTitleTextView().setText("成员申请");
        driverinfo = BaseApplication.getInstance().getDriverInfo();
        menmberlist = new ArrayList();
        adapter = new AcceptMemberAdapter(this, menmberlist);
        lv.setAdapter(adapter);
        getApplicateMenmber();//获取司机的申请列表
    }

    @Override
    protected void initListener() {
        // 设置ListView的滚动监听, 当用户上下拉ListView的时候把已经打开的条目关闭掉
        lv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {
                    // 触摸滑动, 关闭所有已开的条目
                    adapter.closeAllItems();
                }
            }
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
            }
        });
        adapter.setOnCustomListener(new OnCustomListener() {//设置删除事件
            @Override
            public void onCustomerListener(View v, int position) {
                switch (v.getId()) {
                    case R.id.tv_ignore:
                        updateMenmber(position, true);
                        break;
                    case R.id.tv_acceptmenmber_accept:
                        updateMenmber(position, false);
                        break;
                }
            }
        });

    }


    private void updateMenmber(final int position, boolean isIgnore) {//"1"同意加入  "3"忽略 也就是删除
        DialogUtil.showLoadingDialog(activityContext,"请求中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("fleetId", driverinfo.getFleet().getId() + "");
        if (isIgnore) {
            params.put("fleetDriverState", "3");
        } else {
            params.put("fleetDriverState", "1");
        }
        params.put("driverId", driverinfo.getDriverId()+"");
        MyHttpUtil.updateFleetDriver(activityContext,params, new MyCallback<Result>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(Result result, int id) {

                if (result.getCode() == ConstUtil.OK ) {
                    menmberlist.remove(position);
                    adapter.setMenmberlist(menmberlist);
                }else {
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });
    }

    private void getApplicateMenmber() {
        DialogUtil.showLoadingDialog(activityContext,"加载中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("fleetId", driverinfo.getFleet().getId() + "");
        params.put("fleetDriverState", "4");
        MyHttpUtil.selectFleetDriver(activityContext,params, new MyCallback<CarTeamMenmberResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {

                PrintUtil.toast(activityContext, e.getMessage());
            }
            @Override
            public void onSuccess(CarTeamMenmberResult result, int id) {

                if (result.getData().size() > 0) {
                    menmberlist = result.getData();
                    adapter.setMenmberlist(result.getData());
                }
            }
        });

    }
}
